package com.sskj.protocol.sskjboard.handler;

import com.sskj.common.dto.EncodedCommand;
import com.sskj.common.util.DeviceUtil;
import com.sskj.common.vo.req.DeviceCommandReq;
import com.sskj.protocol.sskjboard.model.FrameMessage;
import org.springframework.stereotype.Component;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@Component
public class CommandHandler {

    /**
     * 处理下发命令编码
     * @param command 命令
     * @return 返回最终下发的字节信息
     */
    public EncodedCommand handler(DeviceCommandReq command) {
        if (command.getServiceId().equals("video_playback")) {
            return videoPlayback(command);
        }

        return null;
    }

    /**
     * 视频播放
     * @return 字节
     */
    private EncodedCommand videoPlayback(DeviceCommandReq command) {
        if (command.getParams() == null) {
            return null;
        }

        Map<String, Object> params = command.getParams();
        if (params.get("channel") == null) {
            throw new IllegalArgumentException("无效的通道信息");
        }
        int channel = Integer.parseInt(params.get("channel").toString());
        long startTime = 0;
        long endTime = 0;
        if (params.get("startTime") != null && params.get("endTime") != null) {
            startTime = Long.parseLong(params.get("startTime").toString());
            endTime = Long.parseLong(params.get("endTime").toString());
        }

        byte codeByte = 0x02;
        byte typeByte = 0x02;
        if (channel > 0) {
            byte channelByte = (byte) channel;
            if (startTime > 0 && endTime > 0) {
                // 录播
                byte[] startBytes = ByteBuffer.allocate(8).putLong(startTime).array();
                byte[] endBytes = ByteBuffer.allocate(8).putLong(endTime).array();
                String sessionIdStr = DeviceUtil.buildStreamId(command.getDeviceCode(), channel, command.getUserId());
                byte[] sessionByteIdBytes = sessionIdStr.getBytes(StandardCharsets.UTF_8);

                FrameMessage frameMessage = new FrameMessage();
                frameMessage.setCommandCode(codeByte);
                frameMessage.setCommandType(typeByte);

                ByteBuffer buffer = ByteBuffer.allocate(sessionByteIdBytes.length + 1 + startBytes.length + endBytes.length);
                buffer.put(sessionByteIdBytes);
                buffer.put(channelByte);
                buffer.put(startBytes);
                buffer.put(endBytes);
                byte[] data = buffer.array();
                frameMessage.setData(data);

                Map<String, Object> metadata = new HashMap<>();
                metadata.put("streamId", sessionIdStr);
                return new EncodedCommand(frameMessage.buildFrame(), metadata);
            } else {
                // 点播
                String sessionIdStr = DeviceUtil.buildStreamId(command.getDeviceCode(), channel);
                byte[] sessionIdBytes = sessionIdStr.getBytes(StandardCharsets.UTF_8);

                FrameMessage frameMessage = new FrameMessage();
                frameMessage.setCommandCode(codeByte);
                frameMessage.setCommandType(typeByte);

                ByteBuffer byteBuffer = ByteBuffer.allocate(sessionIdBytes.length + 1);
                byteBuffer.put(sessionIdBytes);
                byteBuffer.put(channelByte);
                frameMessage.setData(byteBuffer.array());

                Map<String, Object> metadata = new HashMap<>();
                metadata.put("streamId", sessionIdStr);

                return new EncodedCommand(frameMessage.buildFrame(), metadata);
            }
        }

        throw new IllegalArgumentException("无效的通道信息");
    }
}
